<?php

class UpdatePostController {
	
	private $m_observerView = "";
	private $m_observerHandler = "";
	private $m_navigationView = "";
	private $m_fileUploadView = "";
	private $m_validate = "";
	private $m_fileUploadHandler = "";
	
	public function __construct ($a_observerView, $a_observerHandler, $a_navigationView, $a_fileUploadView, $a_validate, $a_fileUploadHandler){
		$this->m_observerView = $a_observerView;
		$this->m_observerHandler = $a_observerHandler;
		$this->m_navigationView = $a_navigationView;
		$this->m_fileUploadView = $a_fileUploadView;
		$this->m_validate = $a_validate;
		$this->m_fileUploadHandler = $a_fileUploadHandler;
	}
	
	public function DoControl (){
		// I KNOW, it's a mess. But as a great man once said, "Få det att funka fundera sen på hur det ska se ut". 
		
		$xhtml = "";
		/*
		 *  This a bit tricky I really should break out parts of it but.. here it goes:
		 * If you press update, the post is updated and you are navigated to that category where the post is at. 
		 */
		if($this->m_observerView->TriedToUpdatePost()){
			if($this->m_observerHandler->UpdatePost($this->m_observerView->GetHeader(), $this->m_observerView->GetContent(), $this->m_observerView->GetCurrentPostId())){
			$this->m_navigationView -> NavigateToPost($this->m_observerView->GetCategoryId(), ObserverView::CATEGORYID_URLPARAMETER);
			}
		// If user clicks deletebutton we should delete it right away.
		}elseif($this->m_observerView->TriedToDeletePost()){
			$this->m_observerHandler->DeletePost($this->m_observerView->GetCurrentPostId());
		}
		// If the user tries to upload a image we should.. 
		elseif($this->m_fileUploadView->TriedToUploadImage() && $_FILES['file']['size']!= null){
			//..validate the image. 
			if ($this->m_validate->validateImageUpload()){
				//check if the filename is already taken.
				if ($this->m_fileUploadView -> DoFileExistAlready()) {
					//if it is we should rename it. 
					$this->m_fileUploadView -> RenameFile();
				}
				
				//Save the files. 
				if ($this->m_fileUploadHandler -> SaveUploadedFiles($this->m_fileUploadView -> ReturnFileName(), $this->m_fileUploadView -> ReturnFilePath())) {
						
					// Get content and add the imageTag to it put it in $content
					$content = 	$this->m_observerView->GetContent();
					$content .="<img src='".$this->m_fileUploadView -> ReturnFilePath().
											$this->m_fileUploadView -> ReturnFileName()."'/>";
															
					// Render the updated form with the image tag 
					$xhtml = $this->m_observerView->AddImageEditPost($this->m_observerView->GetHeader(), 
																	$this->m_observerView->GetCurrentPostId(),
																	$content,
																	$this->m_fileUploadView -> GetFileUploadInputs());
				}else{
					// if the save went bad, redo the updateform. Not the best way to handle it.  
					$xhtml .= $this->m_observerView->AddImageEditPost($this->m_observerView->GetHeader(), 
																	$this->m_observerView->GetCurrentPostId(),
																	$content,
																	$this->m_fileUploadView -> GetFileUploadInputs());
				}
			}else {
				$this->m_fileUploadView -> ReturnXhtmlFeedback();
				// Bilden validerades inte!
			}
		}elseif($this->m_fileUploadView->TriedToUploadImage() && $_FILES['file']['size']== null){
				// Get content and add the imageTag to it put it in $content
					$content = 	$this->m_observerView->GetContent();
															
					// Render the updated form with the image tag 
					$xhtml = $this->m_observerView->AddImageEditPost($this->m_observerView->GetHeader(), 
																	$this->m_observerView->GetCurrentPostId(),
																	$content,
																	$this->m_fileUploadView -> GetFileUploadInputs());
		}else{
			$postArray = $this->m_observerHandler->GetSinglePost($this->m_observerView->GetCurrentPostId(), 
														ObserverView::POSTID, 
														ObserverView::POST_HEADER, 
														ObserverView::POST_CONTENT);
																		
			$xhtml = $this->m_observerView->AddImageEditPost($postArray[ObserverView::POST_HEADER], 
														$postArray[ObserverView::POSTID], 
														$postArray[ObserverView::POST_CONTENT], 
														$this->m_fileUploadView -> GetFileUploadInputs()); 
		}
		
		return $xhtml;
	}
}